/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package models;
import java.util.*;
import javax.persistence.*;
import play.data.format.*;
import play.data.validation.*;
import play.db.jpa.*;

@Entity
public class Sorteo {

    @Id
    @Constraints.Required
    public Integer codSorteo;
    
    @Constraints.Required
    public String nomSorteo;
    
    @Formats.DateTime(pattern="yyyy-MM-dd")
    public Date fechaIniRegParticipante;
    
    @Formats.DateTime(pattern="yyyy-MM-dd")
    public Date fechaFinRegParticipante	;
    
    public String estado;
    
    public String metodoSorteo;
    
    @Formats.DateTime(pattern="yyyy-MM-dd")
    public Date fechaSorteo;

    public String metodoSorteo2;  
    
    public static Sorteo findById(Integer codSorteo) {
        return JPA.em().find(Sorteo.class, codSorteo);
    }
    
    public void update(Integer codSorteo) {
        this.codSorteo = codSorteo;
        JPA.em().merge(this);
    }
    
    public void save() {
        System.out.println("Grabando nuevo sorteo...");
        
        JPA.em().persist(this);
    }
    public void delete() {
        JPA.em().remove(this);
    }
    
   
    public static Page page(int page, int pageSize, String sortBy, String order, String filter) {
        if(page < 1) page = 1;
        Long total=0L;
       
            @SuppressWarnings("unchecked")
            List<Sorteo> data = JPA.em()
            .createQuery("from Sorteo s where lower(s.nomSorteo) like ? order by s." + sortBy + " " + order)
            .setParameter(1, "%" + filter.toLowerCase() + "%")
            .setFirstResult((page - 1) * pageSize)
            .setMaxResults(pageSize)
            .getResultList();
            total=Long.parseLong(data.size()+"");
        
        return new Page(data, total, page, pageSize);
    }
    
     public static class Page {
        
        private final int pageSize;
        private final long totalRowCount;
        private final int pageIndex;
        private final List<Sorteo> list;
        
        public Page(List<Sorteo> data, long total, int page, int pageSize) {
            this.list = data;
            this.totalRowCount = total;
            this.pageIndex = page;
            this.pageSize = pageSize;
        }
        
        public long getTotalRowCount() {
            return totalRowCount;
        }
        
        public int getPageIndex() {
            return pageIndex;
        }
        
        public List<Sorteo> getList() {
            return list;
        }
        
        public boolean hasPrev() {
            return pageIndex > 1;
        }
        
        public boolean hasNext() {
            return (totalRowCount/pageSize) >= pageIndex;
        }
        
        public String getDisplayXtoYofZ() {
            int start = ((pageIndex - 1) * pageSize + 1);
            int end = start + Math.min(pageSize, list.size()) - 1;
            return start + " al " + end + " de " + totalRowCount;
        }
        
    }
}
